package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.entity.Sku;
import com.leyou.item.entity.Spu;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    private SkuService skuService;

    @Autowired
    private SpuService spuService;

    @Autowired
    private SpuDetailService spuDetailService;

    /**
     *  分页查询spu
     * @param page  当前页
     * @param rows  每页大小
     * @param saleable  是否上架
     * @param categoryId  商品类型分页
     * @param brandId  品牌id
     * @param id  商品spu的id
     * @return  当前页大小的信息
     */
    @GetMapping("spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> pageSpu(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "brandId", required = false) Long brandId,
            @RequestParam(value = "id", required = false) Long id) {

        PageDTO<SpuDTO> pageSpu = spuService.queryPageSpu(page,rows,saleable,categoryId,brandId,id);
        return ResponseEntity.ok(pageSpu);
    }

    /**
     *  新增商品
     * @param spuDTO  商品信息
     * @return  无
     */
    @PostMapping
    public ResponseEntity<Void> saveSpu(@RequestBody SpuDTO spuDTO){
        //新增商品
        spuService.saveSpu(spuDTO);
        //新增成功200
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     *  是否上架
     * @param id  商品id
     * @param saleable  上架（true）\下架（false）
     * @return 无
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void> isSaleable(@RequestParam("id") Long id ,@RequestParam("saleable") Boolean saleable){
        //修改是否上架
        spuService.updateSaleable(id,saleable);
        //修改成功
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     *  根据id查询spu及sku、spuDetail详情（修改数据前的回显数据）
     * @param spuId  商品id
     * @return spuDto
     */
    @GetMapping("{id}")
    public ResponseEntity<SpuDTO> querySpuAndSpuDetailAndSku(@PathVariable("id") Long spuId ){
        //查询商品详情
        SpuDTO spuDTO = spuService.queryGoodsById(spuId);
        return ResponseEntity.ok(spuDTO);
    }


    /**
     *  修改商品
     * @param spuDTO   商品信息
     * @return 无
     */
    @PutMapping
    public ResponseEntity<Void> updateSpu(@RequestBody SpuDTO spuDTO){
        //修改商品
        spuService.updateSpu(spuDTO);
       //修改成功
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     *  根据skuId批量查询sku
     * @param skuIds skuId
     * @return  sku详情
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>>queryBySkuId(@RequestParam("ids") List<Long> skuIds){
        //通过skuId查询sku
        List<Sku> skuList = skuService.listByIds(skuIds);
        //转换后返回
        return ResponseEntity.ok(SkuDTO.convertEntityList(skuList));
    }

    /**
     *  根据spuID查询sku的集合
     * @param spuId 商品id
     * @return  sku详情
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id")Long spuId){
        //spuID查询sku的集合
        List<SkuDTO> skuDTOS = skuService.queryBySpuId(spuId);
        //返回
        return ResponseEntity.ok(skuDTOS);
    }

    /**
     *  根据商品id查询商品详情
     * @param spuId 商品Id
     * @return 商品详情
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailBySpuId(@RequestParam("id") Long spuId){
        //根据spuId查询商品详情
        SpuDetailDTO detailDTO = spuDetailService.queryBySpuId(spuId);
        //返回
        return ResponseEntity.ok(detailDTO);
    }

    /**
     *  根据spuId查询spu的所有规格参数值
     * @param spuId  商品id
     * @param searching 是否搜索
     * @return 键值对集合
     */
    @GetMapping("spec/value")
    public ResponseEntity<List<SpecParamDTO>> queryParamBySpuIdAndSearching(
            @RequestParam("id") Long spuId ,
            @RequestParam(value = "searching" ,required = false) Boolean searching){
        //查询
        List<SpecParamDTO> list =spuService.queryParamBySpuIdAndSearching(spuId,searching);
        //返回
        return ResponseEntity.ok(list);
    }

    /**
     *  根据商品id查询商品
     * @param spuId  商品id
     * @return
     */
    @GetMapping("spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long spuId){
        //根据spuId查询spu
        Spu spu = spuService.getById(spuId);
        //返回
        return ResponseEntity.ok(new SpuDTO(spu));
    }
}
